package com.xiao.framework.ds.autoconfigure.builder;

import com.xiao.framework.ds.autoconfigure.bean.DataSourceBean;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.util.Optional;

/**
 * @author <a href="mailto:xiaoquanidea@163.com">hutiquan</a>
 * @since 2019-08-24 17:19
 */
public class HikariDataSourceBuilder implements DataSourceBuilder<HikariDataSource> {

  @Override
  public HikariDataSource buildDataSource(String dataSourceName, DataSourceBean dataSourceBean) {
    HikariConfig config = Optional.ofNullable(dataSourceBean.getHikari())
        .orElseGet(HikariConfig::new);

    config.setUsername(dataSourceBean.getUsername());
    config.setPassword(dataSourceBean.getPassword());
    config.setJdbcUrl(dataSourceBean.getUrl());
    config.setDriverClassName(dataSourceBean.getDriverClassName());
    config.setPoolName("HikariCP["+dataSourceName+"]");

    HikariDataSource dataSource = new HikariDataSource(config);
    return dataSource;
  }

}
